//
//  TUIImageMessageCell.m
//  UIKit
//
//  Created by annidyfeng on 2019/5/30.
//

#import "LGImageMessageCell.h"
#import "TUIDefine.h"

@interface LGImageMessageCell ()

@property (nonatomic, strong) UIView *animateHighlightView;

@end

@implementation LGImageMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _thumb = [[UIImageView alloc] init];
        _thumb.layer.cornerRadius = 5.0;
        [_thumb.layer setMasksToBounds:YES];
        _thumb.contentMode = UIViewContentModeScaleAspectFill;
        _thumb.backgroundColor = [UIColor clearColor];
        [self.bubbleView addSubview:_thumb];
        [self.thumb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self.bubbleView);
        }];
        _progress = [[UILabel alloc] init];
        _progress.textColor = [UIColor whiteColor];
        _progress.font = [UIFont systemFontOfSize:15];
        _progress.textAlignment = NSTextAlignmentCenter;
        _progress.layer.cornerRadius = 5.0;
        _progress.hidden = YES;
        _progress.backgroundColor = TImageMessageCell_Progress_Color;
        [_progress.layer setMasksToBounds:YES];
        [self.bubbleView addSubview:_progress];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectMessage)];
        [self.bubbleView addGestureRecognizer:tapGesture];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    NSDictionary *thumDic = [dataDic[@"content"] lastObject];
    CGFloat thumHeight = [thumDic[@"height"] floatValue];
    CGFloat thumWidth = [thumDic[@"width"] floatValue];
    if(thumHeight > thumWidth){
        thumWidth = thumWidth / thumHeight * TImageMessageCell_Image_Height_Max;
        thumHeight = TImageMessageCell_Image_Height_Max;
    } else {
        thumHeight = thumHeight / thumWidth * TImageMessageCell_Image_Width_Max;
        thumWidth = TImageMessageCell_Image_Width_Max;
    }
    [self.thumb sd_setImageWithURL:[NSURL URLWithString: LGNSString(thumDic[@"url"])]];
    [self.bubbleView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(thumWidth));
        make.height.equalTo(@(thumHeight));
    }];
}

-(void)onSelectMessage{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showImageMessage:cell:)]) {
        [self.delegate showImageMessage:self.dataDic cell:self];
    }
}



@end
